<template>
	<div>
		<el-breadcrumb separator="/">
			<el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>公益中心管理</el-breadcrumb-item>
		</el-breadcrumb>
		<!-- 搜索区域 -->
		<el-row>
			<el-form :inline="true">
				<el-form-item label="公益名称">
					<el-input v-model="queryInfo.name" clearable @clear="queryList" placeholder="请输入公益名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="queryList">搜索</el-button>
					<el-button type="primary" @click="toAddFree">新增公益</el-button>
				</el-form-item>
			</el-form>
		</el-row>
		<!-- 表格区域 -->
		<el-table :data="tableData" border style="width: 100%">
			<el-table-column prop="id" label="ID" width="180"> </el-table-column>
			<el-table-column prop="name" label="公益" width="180"> </el-table-column>
			<el-table-column prop="img" label="文章图片">
				<template slot-scope='scope'>
					<el-image style="width: 100px; height: 100px"
						:src="'http://121.41.0.164:8888/sfzy/downLoad?filename='+scope.row.img"></el-image>
				</template>
			</el-table-column>
			<el-table-column prop="simplyIntroduce" label="详情" width="180"> </el-table-column>
			<el-table-column label="操作">
				<template slot-scope="scope">
					<div>
						<el-link type="primary" @click="toEditFree(scope.row)">编辑公益</el-link>
					</div>
					<el-link type="primary" @click="deleteFree(scope.row.id)">删除公益</el-link>
				</template>
			</el-table-column>
		</el-table>
		<!-- 分页区域 -->
		<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
			:current-page="queryInfo.pageNum" :page-sizes="[1, 5, 10, 15, 20]" :page-size="queryInfo.pageSize"
			layout="total, sizes, prev, pager, next, jumper" :total="total">
		</el-pagination>
		<!-- dialog表单对话框 -->
		<el-dialog title="free" :visible.sync="dialogFormVisible" @close="cancel">
			<el-form :model="free">
				<el-form-item label="公益" :label-width="formLabelWidth">
					<el-input v-model="free.name" autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="简介" :label-width="formLabelWidth">
					<el-input v-model="free.simplyIntroduce" autocomplete="off"></el-input>
				</el-form-item>

				<el-form-item label="封面" label-width="80px">
					<!-- <el-input placeholder="请输入文章图片" v-model="article.tabularChart"></el-input> -->
					<!-- 
		 el-upload:上传组件
			action： 上传网址
			list-type：上传组件类型
			on-preview ： 图片预览事件
			on-remove ：移除图片事件
			el-dialog : 预览图片组件
			on-success : 上传成功后的回调函数
			limit  ： 限制上传图片个数
	-->
					<el-upload action="http://121.41.0.164:8888/sfzy/upload" list-type="picture-card"
						:on-success="handleSuccess" :limit="1" :on-preview="handlePictureCardPreview"
						:on-remove="handleRemove" :file-list="fileList">
						<i class="el-icon-plus"></i>
					</el-upload>
					<el-dialog :visible.sync="dialogVisible">
						<img width="100%" :src="dialogImageUrl" alt="">
					</el-dialog>
				</el-form-item>

				<el-form-item label="详情" label-width="80px">
					<!-- 富文本编辑器组件 -->
					<quill-editor v-model="free.text"></quill-editor>
				</el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="cancel">取 消</el-button>
				<el-button type="primary" @click="addFree">确 定</el-button>
			</div>
		</el-dialog>
	</div>
</template>
<script>
	export default {
		data() {
			return {
				dialogImageUrl: '', // 预览图片路径
				dialogVisible: false, // 预览图片的对话框
				formLabelWidth: "40px",
				free: {
					id: '',
					name: '',
					simplyIntroduce: '',
					img: '',
					text: '',
					createTime: ''
				},
				total: "",
				dialogFormVisible: false,
				fileList: [],
				tableData: [],
				queryInfo: {
					name: "",
					pageNum: 1,
					pageSize: 10,
				},
			};
		},
		methods: {
			// 进入修改的预览数据页面
			toEditFree(row) {
				//console.log(row)
				// 赋值
				this.free = row;
				// 设置标题
				this.title = "修改公益";
				// 开启对话框
				this.dialogFormVisible = true;
				// 动态的获取图片的名称并帮到到fileList
				// this.fileList=[{
				// 	name:row.img,
				// 	url:'http://121.41.0.164:8888/sfzy/downLoad?filename='+row.img
				// }];
			},

			// 处理上传成功的函数
			handleSuccess(response) {
				// 把上传后的图片路径，交给文章对象的tabularChart
				this.free.img = response.message;
			},

			// 删除图片的事件
			handleRemove(file, fileList) {
				console.log(file, fileList);
				// 移除数据
				this.free.img = '';
			},

			// 预览图片事件
			handlePictureCardPreview(file) {
				this.dialogImageUrl = file.url;
				this.dialogVisible = true;
			},

			deleteFree(id) {
				this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
						confirmButtonText: "确定",
						cancelButtonText: "取消",
						type: "warning",
					})
					.then(() => {
						console.log("确认删除");
						this.$http
							.delete("free/" + id)
							.then((resp) => {
								if (resp.data.status == 200) {
									this.$message.success(resp.data.message);
									this.queryList();
								} else {
									this.$message.error(resp.data.message);
								}
							})
							.catch((error) => {
								console.error("删除失败:", error);
								this.$message.error("删除失败");
							});
					})
					.catch(() => {
						console.log("取消删除");
						this.$message({
							type: "info",
							message: "已取消删除",
						});
					});
			},

			addFree() {
				if (this.free.id) {
					// 有值为true  修改
					// .........后面实现..............
					this.$http.put("free", this.free).then((resp) => {
						if (resp.data.status == 200) {
							// 消息提示
							this.$message.success("修改成功！");
							// 清空数据
							this.free = {
								// 文章对象
								id: "",
								name: "",
								music: "",
								img: "",
							};
							this.cancel();
							// 关闭对话框
							this.dialogFormVisible = false;
							// 重新查询
							this.queryList();
						} else {
							this.$message.error("接口异常！");
						}
					});
				} else {
					this.$http.post("free", this.free).then((resp) => {
						if (resp.data.status == 200) {
							// 消息提示
							this.$message.success("添加成功！");
							// 清空数据
							this.free = {
								// 文章对象
								id: '',
								name: '',
								simplyIntroduce: '',
								img: '',
								text: '',
								createTime: ''
							};
							this.cancel();
							// 关闭对话框
							this.dialogFormVisible = false;
							// 重新查询
							this.queryList();
						} else {
							this.$message.error("接口异常！");
						}
					});
				}
			},

			cancel() {
				this.dialogFormVisible = false;
				this.free = {};
				this.free.img = '';
				this.fileList = [];
			},
			// toEditFree(row) {
			//   this.free = row;
			//   this.dialogFormVisible = true;
			// },
			toAddFree() {
				this.dialogFormVisible = true;
			},
			handleSizeChange(size) {
				//console.log('每页显示数量发生改变');
				this.queryInfo.pageSize = size;
				this.queryInfo.pageNum = 1;
				// 重新查询
				this.queryList();
			},
			handleCurrentChange(num) {
				//console.log('页码发生改变');
				this.queryInfo.pageNum = num;
				// 重新查询
				this.queryList();
			},
			queryList() {
				// 调用后端接口来获取数据
				this.$http
					.get("/free/find", {
						params: this.queryInfo,
					})
					.then((resp) => {
						if (resp.data.status == 200) {
							console.log(resp.data);
							this.tableData = resp.data.data.list;
							this.total = resp.data.data.total;
							console.log(this.tableData);
						} else {
							this.$message.error("查询失败！");
						}
					});
			},
		},
		mounted() {
			this.queryList();
		},
	};
</script>
<style>
</style>